package cn.com.chengmandian.core.web.config;

import cn.com.chengmandian.core.web.properties.SwaggerProperties;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.*;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Autowired
    private SwaggerProperties swaggerProperties;

    // 访问路径：http://127.0.0.1:xxxx/swagger-ui/index.html
    @Bean
    public Docket createRestApi() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title(swaggerProperties.getTitle())
                .description(swaggerProperties.getDescription())
                .version(swaggerProperties.getVersion())
                .build();
        // coding api文档报错 {'name': 'x-header-client', 'in': 'header', 'description': '客户端标识', 'required': True} is not valid under any of the given schemas
//        List<RequestParameter> params = new ArrayList<>();
//        RequestParameterBuilder clientBuilder = new RequestParameterBuilder();
//        clientBuilder.name(CommonConstant.HEADER_CLIENT_ID)
//                .description("客户端标识")
//                .required(true)
//                .in("header")
//                .accepts(Collections.singleton(MediaType.ALL));
//        params.add(clientBuilder.build());
        return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo)
                .enable(swaggerProperties.isEnable())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build().useDefaultResponseMessages(false);
    }
}
